/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.Universities;
import me.zhengjie.modules.biz.service.UniversitiesService;
import me.zhengjie.modules.biz.domain.vo.UniversitiesQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.List;

import me.zhengjie.utils.StringUtils;
import net.dreamlu.mica.core.result.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
 * @author yiwei.he
 * @date 2024-03-11
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "学校：大学管理")
@RequestMapping("/api/universities")
@Slf4j
public class UniversitiesController {

    private final UniversitiesService universitiesService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/list/download")
//    @PreAuthorize("@el.check()")
    public void exportUniversities(HttpServletResponse response, UniversitiesQueryCriteria criteria) throws IOException {
        log.info("/api/universities  导出数据:{}",criteria);
        universitiesService.download(universitiesService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询大学列表(小程序)")
    @ApiOperation("查询大学（小程序）")
    public ResponseEntity<PageResult<Universities>> queryUniversities(UniversitiesQueryCriteria criteria, Page<Object> page) {
        log.info("/api/universities  查询大学(小程序):{},page:{}",criteria,page);
        return new ResponseEntity<>(universitiesService.queryAll(criteria, page), HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询大学列表(后管)")
    @ApiOperation("查询大学（后管）")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<PageResult<Universities>> list(UniversitiesQueryCriteria criteria, Page<Object> page) {
        log.info("/api/universities 查询大学（后管）:{},page:{}",criteria,page);
        return new ResponseEntity<>(universitiesService.list(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增大学")
    @ApiOperation("新增大学")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> createUniversities(@Validated @RequestBody Universities resources) {
        log.info("/api/universities  新增大学:{}",resources);
        universitiesService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改大学")
    @ApiOperation("修改大学")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> updateUniversities(@Validated @RequestBody Universities resources) {
        log.info("/api/universities  修改大学:{}",resources);
        universitiesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除大学")
    @ApiOperation("删除大学")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> deleteUniversities(@RequestBody List<Long> ids) {
        log.info("/api/universities  删除大学:{}",ids);
        universitiesService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("/detail/{universityName}")
    @Log("院校详情")
    @ApiOperation("院校详情")
    public ResponseEntity<Object> myVolunteer(@PathVariable String universityName) {
        log.info("/api/universities/detail/{}  院校详情",universityName);
        if (null == universityName || StringUtils.isBlank(universityName)) {
            return ResponseEntity.badRequest().body("universityName 参数不能为空");
        }
        return new ResponseEntity<>( universitiesService.queryByName(universityName),HttpStatus.OK);
    }

}